Fix React.act infrastructure issue in jsdom component tests#326
Fix React.act infrastructure issue in jsdom component tests#326brendanlong wants to merge 1 commit intomainfrom
Conversation
The host environment has NODE_ENV=production, which causes React's CJS entry point (react-dom/test-utils.js) to load the production bundle where React.act is not exported. Add an explicit define to override process.env.NODE_ENV to 'test' in the component test config so the development bundle is loaded instead. Fixes #320 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request updates the Vitest component configuration to define the NODE_ENV as 'test'. The reviewer suggests using the test.env configuration instead of the define property to ensure the environment variable is correctly handled across different access patterns, such as destructuring or bracket notation.
| define: { | ||
| 'process.env.NODE_ENV': JSON.stringify('test'), | ||
| }, | ||
| test: { |
There was a problem hiding this comment.
Using define for process.env.NODE_ENV is a literal string replacement that only affects code using the exact process.env.NODE_ENV syntax. It may fail to cover cases where the environment variable is accessed via destructuring (e.g., const { NODE_ENV } = process.env) or bracket notation (e.g., process.env['NODE_ENV']).
For a more robust solution in Vitest, it is recommended to use the test.env configuration. Alternatively, setting the environment variable in the package.json test script (e.g., NODE_ENV=test vitest ...) ensures it is correctly set for the entire process, including the resolver and all dependencies.
| define: { | |
| 'process.env.NODE_ENV': JSON.stringify('test'), | |
| }, | |
| test: { | |
| test: { | |
| env: { | |
| NODE_ENV: 'test', | |
| }, |
Summary
TypeError: React.act is not a functionNODE_ENV=production, which causesreact-dom/test-utils.jsto load the production CJS bundle whereReact.actis not exporteddefine: { 'process.env.NODE_ENV': JSON.stringify('test') }tovitest.component.config.tsso the development bundle is loadedFixes #320
Test plan
pnpm test:component)pnpm test:run)🤖 Generated with Claude Code